//
//  PersonalSituationViewController.m
//  SaltLightDemo
//
//  Created by lzy on 2018/5/8.
//  Copyright © 2018年 lzy. All rights reserved.
//

#import "PersonalSituationViewController.h"
#import "UIButton+WGBCustom.h"
#import "MGRequestService+My.h"
#import "ProductDetailsViewController.h"
#import "SLAboutMeViewController.h"
@interface PersonalSituationViewController ()<UITextFieldDelegate>

@property (nonatomic , strong) UITextField *ysmmTF;
@property (nonatomic , strong) UITextField *xmmTF;
@property (nonatomic , strong) UITextField *cfmmTF;

@property (nonatomic , strong) UIButton *button1;
@property (nonatomic , strong) UIButton *button2;
@property (nonatomic , strong) UIButton *button3;
@property (nonatomic , strong) UIButton *button4;
@property (nonatomic , strong) UIButton *button5;
@property (nonatomic , strong) UIButton *button6;
@property (nonatomic , strong) UIButton *button7;

@property (nonatomic , assign) int workStauts;
@property (nonatomic , assign) int sociaSecurity;
@property (nonatomic , assign) int accumulationFund;
@property (nonatomic , assign) int haveHouse;
@property (nonatomic , assign) int haveCar;

@property (nonatomic , strong) UIButton *backBtn;
@end

@implementation PersonalSituationViewController
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    self.backBtn.hidden = YES;
}
-(void)dealloc
{
    self.backBtn.hidden = YES;
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
   
        
    self.backBtn.hidden = NO;
        
    
}
-(void)goBack
{
    for (int i = 0; i<self.navigationController.viewControllers.count; i++) {
        if ([self.navigationController.viewControllers[i] isKindOfClass:[ProductDetailsViewController class]]) {
            
            [self.navigationController popToViewController:self.navigationController.viewControllers[i] animated:YES];
            return;
        }else if ([self.navigationController.viewControllers[i] isKindOfClass:[SLAboutMeViewController class]]) {
            
            [self.navigationController popToViewController:self.navigationController.viewControllers[i] animated:YES];
            return;
        }else
        {
            [self.navigationController popViewControllerAnimated:YES];
            
        }
    }
    
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    
    self.hideDefaultBackButton = YES;
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backBtn = button1;
    [button1 setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    button1.frame = CGRectMake(17, 0, 50, 44);
    [button1 addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    button1.titleLabel.font = [UIFont systemFontOfSize:16];
    [button1 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [button1 setTitle:@"" forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:button1];
    
    [self initUI];
    
    self.workStauts = 1;
    self.sociaSecurity = 0;
    self.accumulationFund =0;
    self.haveHouse = 0;
    self.haveCar = 0;
    
    // Do any additional setup after loading the view.
}
-(void)initUI{
    UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 165)];
    view1.backgroundColor = [UIColor add_colorWithRGBHexString:@"FFFFFF"];
    [self.view addSubview:view1];
    
    UIView * line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 55, SCREEN_WIDTH, 1.0)];
    line1.backgroundColor = [UIColor add_colorWithRGBHexString:@"EFEFF4"];
    [view1 addSubview:line1];
    
    UIView * line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, 1.0)];
    line2.backgroundColor = [UIColor add_colorWithRGBHexString:@"EFEFF4"];
    [view1 addSubview:line2];
    
    //    1
    UILabel * label1 = [[UILabel alloc]initWithFrame:CGRectMake(25, 10, 100, 35)];
    label1.font = [UIFont boldSystemFontOfSize:14.0];
    label1.text = @"称呼";
    label1.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label1.textAlignment = NSTextAlignmentLeft;
    [view1 addSubview:label1];
    
    UITextField *ysmmTF = [[UITextField alloc] initWithFrame:CGRectMake(105, 17.5, self.view.frame.size.width-150, 20)];
    self.ysmmTF = ysmmTF;
    self.ysmmTF.font = [UIFont systemFontOfSize:14.0];
    //    self.ysmmTF.keyboardType = UIKeyboardTypeNumberPad;
    self.ysmmTF.tag = 1;
    self.ysmmTF.textAlignment = NSTextAlignmentLeft;
    self.ysmmTF.returnKeyType = UIReturnKeyDone;
    self.ysmmTF.placeholder = @"点击输入称呼";
    self.ysmmTF.font = [UIFont systemFontOfSize:14.0];
    [view1 addSubview:self.ysmmTF];
    
    if (UserInfoManage.userInfo.nickName) {
        self.ysmmTF.text = UserInfoManage.userInfo.nickName;
    }
    
    //    2
    UILabel * label2 = [[UILabel alloc]initWithFrame:CGRectMake(25, 10+55, 100, 35)];
    label2.font = [UIFont boldSystemFontOfSize:14.0];
    label2.text = @"年龄";
    label2.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label2.textAlignment = NSTextAlignmentLeft;
    [view1 addSubview:label2];
    
    UITextField *xmmTF = [[UITextField alloc] initWithFrame:CGRectMake(105, 17.5+55, self.view.frame.size.width-150, 20)];
    self.xmmTF.delegate = self;
    self.xmmTF = xmmTF;
    self.xmmTF.font = [UIFont systemFontOfSize:14.0];
    self.xmmTF.keyboardType = UIKeyboardTypeNumberPad;
    self.xmmTF.tag = 2;
    self.xmmTF.textAlignment = NSTextAlignmentLeft;
    self.xmmTF.returnKeyType = UIReturnKeyDone;
    self.xmmTF.placeholder = @"点击输入年龄";
    self.xmmTF.font = [UIFont systemFontOfSize:14.0];
    [view1 addSubview:self.xmmTF];
    if (UserInfoManage.userInfo.age) {
        self.xmmTF.text = [NSString stringWithFormat:@"%d",UserInfoManage.userInfo.age];
    }
    //    3
    UILabel * label3 = [[UILabel alloc]initWithFrame:CGRectMake(25, 10+110, 75, 35)];
    label3.font = [UIFont boldSystemFontOfSize:14.0];
    label3.text = @"工作状态：";
    label3.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label3.textAlignment = NSTextAlignmentLeft;
    [view1 addSubview:label3];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.button1 = btn1;
    btn1.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [btn1 setTitle:@"上班族" forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"app_icon_radio1"] forState:UIControlStateNormal];
    btn1.frame = CGRectMake(105, 110+17.5, (SCREEN_WIDTH-100-25)/3, 20);
    [btn1 addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:btn1];
    [btn1 imageOnTheTitleLeftWithSpace:5];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
    self.button2 = btn2;
    btn2.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [btn2 setTitle:@"企业主" forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
    btn2.frame = CGRectMake(105+(SCREEN_WIDTH-100-25)/3, 110+17.5, (SCREEN_WIDTH-100-25)/3, 20);
    [btn2 addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:btn2];
    [btn2 imageOnTheTitleLeftWithSpace:5];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn3 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
    self.button3 = btn3;
    btn3.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [btn3 setTitle:@"无业" forState:UIControlStateNormal];
    [btn3 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
    btn3.frame = CGRectMake(105+(SCREEN_WIDTH-100-25)*2/3, 110+17.5, (SCREEN_WIDTH-100-25)/3, 20);
    [btn3 addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:btn3];
    [btn3 imageOnTheTitleLeftWithSpace:5];
    
    if (UserInfoManage.userInfo.workStauts == 1) {
        [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.button1 setImage:[UIImage imageNamed:@"app_icon_radio1"] forState:UIControlStateNormal];
        [self.button2 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button2 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
        [self.button3 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button3 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
    }else if (UserInfoManage.userInfo.workStauts == 2) {
        [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.button2 setImage:[UIImage imageNamed:@"app_icon_radio1"] forState:UIControlStateNormal];
        [self.button1 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button1 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
        [self.button3 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button3 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
    }else if (UserInfoManage.userInfo.workStauts == 3) {
        [self.button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.button3 setImage:[UIImage imageNamed:@"app_icon_radio1"] forState:UIControlStateNormal];
        [self.button2 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button2 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
        [self.button1 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button1 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
    }
    
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(view1.frame)+10, SCREEN_WIDTH, 220)];
    view2.backgroundColor = [UIColor add_colorWithRGBHexString:@"FFFFFF"];
    [self.view addSubview:view2];
    
    UILabel * label4 = [[UILabel alloc]initWithFrame:CGRectMake(25, 10, 75, 35)];
    label4.font = [UIFont boldSystemFontOfSize:14.0];
    label4.text = @"社保：";
    label4.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label4.textAlignment = NSTextAlignmentLeft;
    [view2 addSubview:label4];
    
    UIButton * btn4 = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-55, 20, 30, 14)];
    self.button4 = btn4;
    [btn4 setImage:[UIImage imageNamed:@"Rectangle1"] forState:UIControlStateNormal];
    [btn4 addTarget:self action:@selector(tapAction1:) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:btn4];
    if (UserInfoManage.userInfo.sociaSecurity == 1){
        [btn4 setImage:[UIImage imageNamed:@"Rectangle2"] forState:UIControlStateNormal];
    }
    
    UIView * line3 = [[UIView alloc]initWithFrame:CGRectMake(0, 55, SCREEN_WIDTH, 1.0)];
    line3.backgroundColor = [UIColor add_colorWithRGBHexString:@"EFEFF4"];
    [view2 addSubview:line3];
    
    UILabel * label5 = [[UILabel alloc]initWithFrame:CGRectMake(25, 10+55, 75, 35)];
    label5.font = [UIFont boldSystemFontOfSize:14.0];
    label5.text = @"有公积金：";
    label5.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label5.textAlignment = NSTextAlignmentLeft;
    [view2 addSubview:label5];
    
    UIButton * btn5 = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-55, 20+55, 30, 14)];
    self.button5 = btn5;
    [btn5 setImage:[UIImage imageNamed:@"Rectangle1"] forState:UIControlStateNormal];
    [btn5 addTarget:self action:@selector(tapAction1:) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:btn5];
    if (UserInfoManage.userInfo.accumulationFund == 1){
        [btn5 setImage:[UIImage imageNamed:@"Rectangle2"] forState:UIControlStateNormal];
    }
    
    UIView * line4 = [[UIView alloc]initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH, 1.0)];
    line4.backgroundColor = [UIColor add_colorWithRGBHexString:@"EFEFF4"];
    [view2 addSubview:line4];
    
    UILabel * label6 = [[UILabel alloc]initWithFrame:CGRectMake(25, 10+110, 75, 35)];
    label6.font = [UIFont boldSystemFontOfSize:14.0];
    label6.text = @"有房：";
    label6.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label6.textAlignment = NSTextAlignmentLeft;
    [view2 addSubview:label6];
    
    UIButton * btn6 = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-55, 20+110, 30, 14)];
    self.button6 = btn6;
    [btn6 setImage:[UIImage imageNamed:@"Rectangle1"] forState:UIControlStateNormal];
    [btn6 addTarget:self action:@selector(tapAction1:) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:btn6];
    if (UserInfoManage.userInfo.haveHouse == 1){
        [btn6 setImage:[UIImage imageNamed:@"Rectangle2"] forState:UIControlStateNormal];
    }
    
    UIView * line5 = [[UIView alloc]initWithFrame:CGRectMake(0, 165, SCREEN_WIDTH, 1.0)];
    line5.backgroundColor = [UIColor add_colorWithRGBHexString:@"EFEFF4"];
    [view2 addSubview:line5];
    
    UILabel * label7 = [[UILabel alloc]initWithFrame:CGRectMake(25, 10+165, 75, 35)];
    label7.font = [UIFont boldSystemFontOfSize:14.0];
    label7.text = @"有车：";
    label7.textColor = [UIColor add_colorWithRGBHexString:@"333333"];
    label7.textAlignment = NSTextAlignmentLeft;
    [view2 addSubview:label7];
    
    UIButton * btn7 = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-55, 20+165, 30, 14)];
    self.button7 = btn7;
    [btn7 setImage:[UIImage imageNamed:@"Rectangle1"] forState:UIControlStateNormal];
    [btn7 addTarget:self action:@selector(tapAction1:) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:btn7];
    if (UserInfoManage.userInfo.haveCar == 1){
        [btn7 setImage:[UIImage imageNamed:@"Rectangle2"] forState:UIControlStateNormal];
    }
    
    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 250)/2.0, CGRectGetMaxY(view2.frame)+50, 250, 40)];
    button.backgroundColor = [UIColor add_colorWithRGBHexString:@"FFD600"];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 20;
    [self.view addSubview:button];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChange) name:UITextFieldTextDidChangeNotification object:nil];
}
-(void)textFieldTextChange
{
    //限制texitfiled的长度
    [Util limitTextField:self.ysmmTF Length:10];
   
    [Util limitTextField:self.xmmTF Length:2];
    
}
#pragma mark -- UITextField delegate --
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField==self.xmmTF){
        return [self validateNumber:string];
    }
    return YES;
    
}
- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789０１２３４５６７８９"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

-(void)tapAction1:(UIButton * )btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        [btn setImage:[UIImage imageNamed:@"Rectangle2"] forState:UIControlStateNormal];
        if (btn == self.button4) {
            self.sociaSecurity = 1;
        }else if (btn == self.button5) {
            self.accumulationFund = 1;
        }else if (btn == self.button6) {
            self.haveHouse = 1;
        }else if (btn == self.button7) {
            self.haveCar = 1;
        }
    }else
    {
        [btn setImage:[UIImage imageNamed:@"Rectangle1"] forState:UIControlStateNormal];
        if (btn == self.button4) {
            self.sociaSecurity = 2;
        }else if (btn == self.button5) {
            self.accumulationFund = 2;
        }else if (btn == self.button6) {
            self.haveHouse = 2;
        }else if (btn == self.button7) {
            self.haveCar = 2;
        }
    }
}
-(void)tapAction:(UIButton * )btn{
    if (btn == self.button1) {
        
        self.workStauts = 1;
        [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.button1 setImage:[UIImage imageNamed:@"app_icon_radio1"] forState:UIControlStateNormal];
        [self.button2 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button2 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
        [self.button3 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button3 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
    }else if (btn == self.button2) {
        self.workStauts = 2;
        [self.button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.button2 setImage:[UIImage imageNamed:@"app_icon_radio1"] forState:UIControlStateNormal];
        [self.button1 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button1 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
        [self.button3 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button3 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
    }else if (btn == self.button3) {
        self.workStauts = 3;
        [self.button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.button3 setImage:[UIImage imageNamed:@"app_icon_radio1"] forState:UIControlStateNormal];
        [self.button2 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button2 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
        [self.button1 setTitleColor:[UIColor add_colorWithRGBHexString:@"CCCCCC"] forState:UIControlStateNormal];
        [self.button1 setImage:[UIImage imageNamed:@"app_icon_radio2"] forState:UIControlStateNormal];
    }
}
-(void)back:(UIButton * )btn{
    
   [self.view endEditing:YES];

    if (self.ysmmTF.text <= 0) {
       [self showToastInViewController:@"请输入称呼"];
    }
    if (self.xmmTF.text <= 0 || [self.xmmTF.text intValue]<=0) {
        [self showToastInViewController:@"请输入正确的年龄"];
    }
    
    UserInfoManage.userInfo.nickName = self.ysmmTF.text;
    UserInfoManage.userInfo.age = [self.xmmTF.text intValue];
    UserInfoManage.userInfo.workStauts = self.workStauts;
    UserInfoManage.userInfo.sociaSecurity = self.sociaSecurity;
    UserInfoManage.userInfo.accumulationFund = self.accumulationFund;
    UserInfoManage.userInfo.haveHouse = self.haveHouse;
    UserInfoManage.userInfo.haveCar = self.haveCar;
    
    
    [MGRequestService requestupdateUserInfoWithUserInfo:UserInfoManage.userInfo success:^(MGBaseRspModel *model) {
        if(model.resultCode == 0 ){
            UserInfoManage.userInfo = UserInfoManage.userInfo;
            [self showToastInViewController:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else
        {
            [self showToastInViewController:model.errorMessage];
        }
    } failure:^(NSError *error) {
        [self showToastInViewController:@"提交失败"];
    }];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
